package com.ledim.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.ledim.activity.base.LeDimBaseActivity;
import com.ledim.adapter.GoneCardsAdapter;
import com.ledim.bean.LedimChoiceCardBean;
import com.ledim.db.LedimDBUtils;
import com.ledim.fragment.CommonDialogFragment;
import com.ledim.itf.LedimDialogClickListner;
import com.ledim.itf.LedimOnItemClickListener;
import com.ledim.utils.ConstantUtil;
import com.letv.android.young.client.R;
import java.util.List;

/**
 * 观看历史页
 */
public class GoneCardsActivity extends LeDimBaseActivity
        implements LedimOnItemClickListener<LedimChoiceCardBean> {

    private ImageView mBack;
    private TextView mSelectAll;
    private TextView mDelete;
    private TextView mEditButton;
    private ListView mListView;
    private View mDeleteLayout;
    private GoneCardsAdapter mCardsAdapter;
    private View mNoCardsLayout;
    private CommonDialogFragment mClearDialog;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_gone_cards;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        mBack = (ImageView) findViewById(R.id.gone_cards_back);
        mEditButton = (TextView) findViewById(R.id.gone_cards_edit);
        mSelectAll = (TextView) findViewById(R.id.gone_cards_select_all);
        mDelete = (TextView) findViewById(R.id.gone_cards_delete);
        mListView = (ListView) findViewById(R.id.listView_cards);
        mDeleteLayout = findViewById(R.id.edit_button_layout);
        mNoCardsLayout = findViewById(R.id.no_gone_cards_layout);

        mBack.setOnClickListener(this);
        mEditButton.setOnClickListener(this);
        mSelectAll.setOnClickListener(this);
        mDelete.setOnClickListener(this);
        mDeleteLayout.setVisibility(View.GONE);
        mNoCardsLayout.setVisibility(View.GONE);

    }

    @Override
    protected void onResume() {
        super.onResume();
        showAllGoneCards();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.gone_cards_back:
                finish();
                break;
            case R.id.gone_cards_edit:
                if (mCardsAdapter != null && mCardsAdapter.getCount() > 0) {
                    if (mEditButton.getText().toString().trim().equals("编辑")) {
                        mEditButton.setText("取消");
                        mSelectAll.setText("全部选择");
                        mDelete.setText("删除");
                        mDelete.setBackgroundResource(R.drawable.rect_round_start_play_bg);
                        mDeleteLayout.setVisibility(View.VISIBLE);
                        mCardsAdapter.mSelectedType = true;
                        mCardsAdapter.notifyDataSetChanged();

                    } else if (mEditButton.getText().toString().trim().equals("取消")) {
                        mEditButton.setText("编辑");
                        mCardsAdapter.getSelectedCards().clear();
                        mDeleteLayout.setVisibility(View.GONE);
                        mCardsAdapter.mSelectedType = false;
                        mCardsAdapter.notifyDataSetChanged();
                    }
                }
                break;
            case R.id.gone_cards_select_all:
                if (mSelectAll.getText().equals("全部选择")) {
                    mCardsAdapter.selectAll();
                    mDelete.setText("删除（" + mCardsAdapter.getSelectedCount() + "）");
                    mDelete.setBackgroundResource(R.drawable.rect_round_delete_play_list_bg);
                    mSelectAll.setText("取消全部");
                } else if (mSelectAll.getText().equals("取消全部")) {
                    mCardsAdapter.getSelectedCards().clear();
                    mCardsAdapter.notifyDataSetChanged();
                    mDelete.setText("删除");
                    mDelete.setBackgroundResource(R.drawable.rect_round_start_play_bg);
                    mSelectAll.setText("全部选择");
                }
                break;
            case R.id.gone_cards_delete:
                if (mCardsAdapter.getSelectedCount() > 0) {
                    showClearDialog();
                }
                break;
        }
    }

    /**
     * 二次确认清除缓存弹窗
     */
    private void showClearDialog() {
        if (mClearDialog == null) {
            mClearDialog =
                    CommonDialogFragment.getInstance(ConstantUtil.DialogConstant.DIALOG_HORIZONTAL_TYPE, "清除观看历史：除秽之刃，斩！", "确定", "取消", new LedimDialogClickListner() {
                        @Override
                        public void onClickPositive(View view) {
                            LedimDBUtils.deleteAllChoiceCards(mCardsAdapter.getSelectedCards());
                            mCardsAdapter.clearCards();
                            initViewByCardsCount(mCardsAdapter.getCount());
                            mDelete.setText("删除");
                            mDelete.setBackgroundResource(R.drawable.rect_round_start_play_bg);
                        }

                        @Override
                        public void onClickNegative(View view) {

                        }

                        @Override
                        public void onDismiss() {

                        }
                    });
        }
        mClearDialog.showDialog(getSupportFragmentManager());

    }

    //显示观看历史
    private void showAllGoneCards() {
        try {
            List cardLists = LedimDBUtils.queryChoiceCards();
            initViewByCardsCount(cardLists.size());
            if (cardLists.size() > 0) {
                if (mCardsAdapter == null) {
                    mCardsAdapter = new GoneCardsAdapter(this, this, cardLists, false);
                    mListView.setAdapter(mCardsAdapter);
                } else {
                    mCardsAdapter.setDatas(cardLists);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initViewByCardsCount(int cardCount) {
        if (cardCount == 0) {
            mListView.setVisibility(View.GONE);
            mNoCardsLayout.setVisibility(View.VISIBLE);
            mDeleteLayout.setVisibility(View.GONE);
            mEditButton.setText("编辑");
        } else {
            mNoCardsLayout.setVisibility(View.GONE);
            mListView.setVisibility(View.VISIBLE);
        }
    }
    @Override
    public void onItemClick(int position, LedimChoiceCardBean item, View v) {
        if (mCardsAdapter.getSelectedCount() > 0) {
            mDelete.setText("删除（" + mCardsAdapter.getSelectedCount() + "）");
            mDelete.setBackgroundResource(R.drawable.rect_round_delete_play_list_bg);
            if (mCardsAdapter.isSelectedAll()) {
                mSelectAll.setText("取消全部");
            } else {
                mSelectAll.setText("全部选择");
            }

        } else {
            mSelectAll.setText("全部选择");
            mDelete.setText("删除");
            mDelete.setBackgroundResource(R.drawable.rect_round_start_play_bg);
        }
    }
}
